
namespace FastFood.DataAccess
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.Common;
	using Microsoft.Practices.EnterpriseLibrary.Data;
	using EDOSwit.Entity;
    using FastFood.Business.Entities;

	/// <summary>
	/// Represents the DataAccess of Combos_InsumosData.
	/// </summary>
	public class Combos_InsumosData : Singleton<Combos_InsumosData>
	{
		#region Attributes

		private readonly Database BaseDatos = DatabaseFactory.CreateDatabase();

		#endregion

		#region Methods

		/// <summary>
		/// Selects all records from the Combos_Insumos table by a foreign key.
		/// </summary>
		public IList<Combos_Insumos> Listar()
		{
			var entidades = new List<Combos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectAllCombos_Insumos");

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new Combos_Insumos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
						cantidad = lector.IsDBNull(lector.GetOrdinal("cantidad"))? default(byte) : lector.GetByte(lector.GetOrdinal("cantidad"))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the Combos_Insumos table by a foreign key.
		/// </summary>
		public Combos_Insumos Obtener(short comboID, short insumoID)
		{
			var combos_Insumos = default(Combos_Insumos);
			var comando = BaseDatos.GetStoredProcCommand("SelectCombos_Insumos");

			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					combos_Insumos = new Combos_Insumos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
						cantidad = lector.IsDBNull(lector.GetOrdinal("cantidad"))? default(byte) : lector.GetByte(lector.GetOrdinal("cantidad"))
					};
				}
			}
			comando.Dispose();
			return combos_Insumos;
		}

		/// <summary>
		/// Saves a record to the Combos_Insumos table.
		/// </summary>
        public int Agregar(short comboID, Combos_Insumos combos_Insumos, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("InsertCombos_Insumos");
			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, combos_Insumos.insumoID);
			BaseDatos.AddInParameter(comando, "cantidad", DbType.Byte, combos_Insumos.cantidad);

			var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
			if (resultado == 0) throw new Exception("Error al Agregar Combos_Insumos.");

			comando.Dispose();
			return 1;
		}

		/// <summary>
		/// Updates a record in the Combos_Insumos table.
		/// </summary>
		public bool Modificar(Combos_Insumos combos_Insumos)
		{
			var comando = BaseDatos.GetStoredProcCommand("UpdateCombos_Insumos");
			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, combos_Insumos.comboID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, combos_Insumos.insumoID);
			BaseDatos.AddInParameter(comando, "cantidad", DbType.Byte, combos_Insumos.cantidad);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Modificar Combos_Insumos.");

			comando.Dispose();
			return true;
		}

		/// <summary>
		/// Deletes a record from the Combos_Insumos table by its primary key.
		/// </summary>
		public bool Eliminar(short comboID, short insumoID)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteCombos_Insumos");
			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Eliminar Combos_Insumos.");
			comando.Dispose();
			return true;
		}

		/// <summary>
		/// Deletes a record from the Combos_Insumos table by a foreign key.
		/// </summary>
		public void EliminarPorComboID(short comboID, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteCombos_InsumosByComboID");
			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);

            var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
			if (resultado == 0) throw new Exception("Error al Eliminar Combos_Insumos.");
			comando.Dispose();
		}

		/// <summary>
		/// Deletes a record from the Combos_Insumos table by a foreign key.
		/// </summary>
		public void EliminarPorInsumoID(short insumoID)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteCombos_InsumosByInsumoID");
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Eliminar Combos_Insumos.");
			comando.Dispose();
		}

		/// <summary>
		/// Selects all records from the Combos_Insumos table by a foreign key.
		/// </summary>
		public IList<Combos_Insumos> ListarPorComboID(short comboID)
		{
			var entidades = new List<Combos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectCombos_InsumosByComboID");

			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new Combos_Insumos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
						cantidad = lector.IsDBNull(lector.GetOrdinal("cantidad"))? default(byte) : lector.GetByte(lector.GetOrdinal("cantidad"))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the Combos_Insumos table by a foreign key.
		/// </summary>
		public IList<Combos_Insumos> ListarPorInsumoID(short insumoID)
		{
			var entidades = new List<Combos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectCombos_InsumosByInsumoID");

			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new Combos_Insumos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
						cantidad = lector.IsDBNull(lector.GetOrdinal("cantidad"))? default(byte) : lector.GetByte(lector.GetOrdinal("cantidad"))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		#endregion
	}
}
